import api from "../../../common/api";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    upOrDown: Boolean, //是上架中还是下架
    house: Object, //列表数据
    isShowFocusFTQR: Boolean, //控制弹窗的显示
  },

  /**
   * 组件的初始数据
   */
  data: {
    moreList: [], //更多已下架房态
    pageIndex: 0, //当前页
    hasMoreList: true,
    selectIdList: [],
    isCoopertion: false, //是否出现已合作背景图or出现更多下架按钮
    notCooperationPng:false, //争对不可合作的图标
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {},
    detached: function () {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noop(){},
    //点击X
    clsoe() {
      let houseID = this.data.house.houseID
      this.setData({
        isShowFocusFTQR: false,
        isCoopertion:false,
        notCooperationPng:false,
        moreList:[],
        hasMoreList:true,
        pageIndex:0
      })
      wx.navigateTo({
        url: `/pages/visitorsRecord/visitorsRecord?cooperationId=${houseID}`,
      })
    },
    //不可合作,扔下架
    stillShelvesHouse(){
      this.clsoe()
    },
    // 点击不可合作
    notCooperation() {
      return new Promise((resolve, reject) => {
        let {houseID}= this.data.house
        api.downMyCooperation({cooperationId:houseID},(res)=>{
          if(res && res.data && res.data.isSuccess){
            this.cooperation('notCooperationPng')
            resolve()
          }
        })
      })

    },
    // 房子仍可合作
    cooperation(no) {
      return new Promise((resolve, reject) => {
        let { confirmId,houseID} = this.data.house
        api.houseStatusConfirm({confirmId,houseCooperationId: houseID}, (res) => {
          if (res && res.data && res.data.isSuccess) {
            let isCoopertion = res.data.content //为true展示更多， false：关闭弹窗
            if (isCoopertion) {
              if(no=='notCooperationPng'){
                this.setData({
                  notCooperationPng: true
                })
              }
              this.setData({
                isCoopertion: true
              })
            } else {
              this.clsoe()
            }
            resolve()
          } else {
            wx.showToast({
              title: res.data.errorMessage,
              icon: "none",
            })
            reject()
          }
        })
      })
    },
    // 点击 '更多即将下架房源房态确认'
    moreBut() {
      return new Promise((resolve, reject) => {
        let currentIndex = this.data.pageIndex + 1;
        let that = this
        if (!that.data.hasMoreList) {
          resolve();
          return;
        }
        wx.showLoading({
          title: "加载中...",
        });
        api.searchMyCooperationComingDown({page: currentIndex, pageSize: 3}, (res) => {
          if (res && res.data && res.data.isSuccess) {
            let list = res.data.content.rows || []
            let maxIndex = res.data.content.total
            let formatList = list.map((item) => {
              return {
                houseType: item.delegationType,
                houseID: item.cooperationId || "",
                houseName: item.resblockName || "",
                houseImg: item.imgId || "",
                houseArea: item.buildSize ? item.buildSize + "㎡" : "--",
                houseRoom: item.room || 0,
                houseHall: item.hall || 0,
                houseDistrict: item.area || "",
                housePrice: item.delegationType === "SALES" ? (item.totalPrice) / 10000 : item.totalPrice,
                title: item.title, //标题
                consultNum: item.consultNum || 0, //访问量
                visitNum: item.visitNum || 0, //咨询量
                isCheck: true //列表是否出现checkBox按钮

              };
            });
            if (currentIndex === 1) {
              that.data.moreList = [...formatList];
            } else {
              that.data.moreList.push(...formatList);
            }
            that.setData({
              pageIndex: currentIndex,
              hasMoreList: currentIndex >= maxIndex ? false : true,
              moreList: that.data.moreList
            });
            wx.hideLoading();
            resolve()
          } else {
            wx.hideLoading();
            wx.showToast({
              title: res.data.errorMessage,
              icon: "none",
            })
            reject()
          }
        })
      })
    },

    // 查看更多
    lookMore() {
      this.moreBut()
    },

    //列表选中和取消
    checkbox(e) {
      let id = e.detail
      if (this.data.selectIdList.indexOf(id) > -1) {
        this.data.selectIdList = this.data.selectIdList.filter(v => v != id)
        this.setData({
          selectIdList: this.data.selectIdList
        })
      } else {
        this.data.selectIdList.push(id)
        this.setData({
          selectIdList: this.data.selectIdList
        })
      }
      this.data.moreList.forEach((item) => {
        item.showTips = this.data.selectIdList.includes(item.houseID)
        item.showCheckbox = this.data.selectIdList.includes(item.houseID)
      })
      this.setData({
        moreList: this.data.moreList
      })
    },
    // 确认可合作(x)套
    confirmMore() {
      return new Promise((resolve, reject) => {
        let houseCooperationIds = this.data.selectIdList
        if(this.data.selectIdList.length!==0){ //判断是否有勾选
          api.conFirmComingDown({houseCooperationIds}, (res) => {
            if (res && res.data && res.data.isSuccess) {
              this.clsoe()
              resolve()
            } else {
              wx.showToast({
                title: '接口问题',
                icon: "none",
              })
              reject()
            }
          })
        } else{
          console.log('没有选择房源')
          reject()
        }
      })
    },
    // 可合作，重新上架
    upCooperation() {
      return new Promise((resolve, reject) => {
        let houseOldID = [this.data.house.houseOldID]
        api.publishCooperationHouse(houseOldID, ({data}) => {
            if (data&&data.isSuccess) {
              this.clsoe()
              resolve();
            } else {
              wx.showToast({
                title: '接口问题',
                icon: "none",
              })
              reject();
            }
          }
        );
      });
    },
  }
})